<?php
namespace app\index\controller;
use app\BaseController;
use think\Controller;
use think\request;
use think\facade\Db;
class Wx extends BaseController {
    public function test(){
        $orderlist =Db::name('cxyun_pay_bill')
            ->order("id desc")
            ->select();
        $list=[];
        echo "<pre>";
        foreach ($orderlist as $info){
            $order_info=Db::table("cxyun_order")->where("tradeNo",$info["trade_no"])->find();
            Db::table("cxyun_pay_bill")->where("id",$info["id"])->update([
                'parkingNo'=>$order_info["parkingNo"],
            ]);
        }
    }
	public function wxget(){
		$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx352362f198d9c56b&redirect_uri=http://www.fqsc.zfbzhsq.com/index/Wx/getuserinfo.html&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
		header("Location:".$url);
		exit;
	}
	
	public function getuserinfo(){
		$code = request()->get('code');
		$appid="wx352362f198d9c56b";
		$secret="891f9c8031c893848960d665e218f9ce";     
		//用通过code换取网页授权access_token，同时获得openid
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
		$userinfo=$this->getJson($url);
		//var_dump($userinfo);die;
		if($userinfo){
			session("openid",$userinfo["openid"]);
			header("Location:".session('url'));
			exit;
		}else{
			return "获取用户信息失败";
		}
	}
	function getJson($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    } 
}